<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use EasyWeChat;
use Log;

class IndexController extends Controller
{
    public function getOpenid(Request $request)
    {
        $rs['status'] = false;
        $code = trim($request->code);
        $mini = EasyWeChat::miniProgram(); // 小程序
        $result = $mini->auth->session($code); // $code 为wx.login里的code
        Log::info('wecaht信息: '.json_encode($result));
        if(!array_key_exists('openid', $result)) {
            $rs['msg'] = $result['errmsg'];
            return response()->json($rs);
        }
        
        $rs['status'] = true;
        $rs['data']['openid'] = $result['openid'];

       
        return response()->json($rs);   
    }
}
